﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.Ajax;
using System.Web.Routing;

namespace Share.Bonnet.Pager {
    public static class ActionLinkExtentions {

        /// <summary>
        /// Overload ActionLink to allow the use of HTML by
        /// wrapping in the HtmlContainer class
        /// </summary>
        /// <param name="AjaxHelper"></param>
        /// <param name="Html"></param>
        /// <param name="Action"></param>
        /// <param name="RouteValues"></param>
        /// <param name="AjaxOptions"></param>
        /// <param name="HtmlAttributes"></param>
        /// <param name="MergeRouteValuesWithCurrent"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink(this AjaxHelper AjaxHelper, HtmlContainer Html, string Action, object RouteValues = null, AjaxOptions AjaxOptions = null, IDictionary<string, object> HtmlAttributes = null, bool MergeRouteValuesWithCurrent = true) {
            return AjaxHelper.ActionLink(Html, Action, null, new RouteValueDictionary(RouteValues), AjaxOptions, HtmlAttributes, MergeRouteValuesWithCurrent);
        }

        /// <summary>
        /// Overload ActionLink to allow the use of HTML by
        /// wrapping in the HtmlContainer class
        /// </summary>
        /// <param name="AjaxHelper"></param>
        /// <param name="Html"></param>
        /// <param name="Action"></param>
        /// <param name="RouteValues"></param>
        /// <param name="AjaxOptions"></param>
        /// <param name="HtmlAttributes"></param>
        /// <param name="MergeRouteValuesWithCurrent"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink(this AjaxHelper AjaxHelper, HtmlContainer Html, string Action, RouteValueDictionary RouteValues = null, AjaxOptions AjaxOptions = null, IDictionary<string, object> HtmlAttributes = null, bool MergeRouteValuesWithCurrent = true) {
            return AjaxHelper.ActionLink(Html, Action, null, RouteValues, AjaxOptions, HtmlAttributes, MergeRouteValuesWithCurrent);
        }

        /// <summary>
        /// Overload ActionLink to allow the use of HTML by
        /// wrapping in the HtmlContainer class
        /// </summary>
        /// <param name="AjaxHelper"></param>
        /// <param name="Html"></param>
        /// <param name="Action"></param>
        /// <param name="Controller"></param>
        /// <param name="RouteValues"></param>
        /// <param name="AjaxOptions"></param>
        /// <param name="HtmlAttributes"></param>
        /// <param name="MergeRouteValuesWithCurrent"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink(this AjaxHelper AjaxHelper, HtmlContainer Html, string Action, string Controller, object RouteValues = null, AjaxOptions AjaxOptions = null, IDictionary<string, object> HtmlAttributes = null, bool MergeRouteValuesWithCurrent = true) {
            return AjaxHelper.ActionLink(Html, Action, Controller, new RouteValueDictionary(RouteValues), AjaxOptions, HtmlAttributes, MergeRouteValuesWithCurrent);
        }

        /// <summary>
        /// Overload ActionLink to allow the use of HTML by
        /// wrapping in the HtmlContainer class
        /// </summary>
        /// <param name="AjaxHelper"></param>
        /// <param name="Html"></param>
        /// <param name="Action"></param>
        /// <param name="Controller"></param>
        /// <param name="RouteValues"></param>
        /// <param name="AjaxOptions"></param>
        /// <param name="HtmlAttributes"></param>
        /// <param name="MergeRouteValuesWithCurrent"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink(this AjaxHelper AjaxHelper, HtmlContainer Html, string Action, string Controller, RouteValueDictionary RouteValues = null, AjaxOptions AjaxOptions = null, IDictionary<string, object> HtmlAttributes = null, bool MergeRouteValuesWithCurrent = true) {
            string Holder = Guid.NewGuid().ToString();
            string Anchor = AjaxHelper.ActionLink(Holder, Action, Controller, RouteValues, AjaxOptions, HtmlAttributes).ToString();

            return MvcHtmlString.Create(Anchor.Replace(Holder, Html.ToString()));
        }

        /// <summary>
        /// Overload ActionLink to allow the use of HTML by
        /// wrapping in the HtmlContainer class
        /// </summary>
        /// <param name="HtmlHelper"></param>
        /// <param name="Html"></param>
        /// <param name="Action"></param>
        /// <param name="RouteValues"></param>
        /// <param name="HtmlAttributes"></param>
        /// <param name="MergeRouteValuesWithCurrent"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink(this HtmlHelper HtmlHelper, HtmlContainer Html, string Action, object RouteValues = null, IDictionary<string, object> HtmlAttributes = null, bool MergeRouteValuesWithCurrent = true) {
            return HtmlHelper.ActionLink(Html, Action, null, new RouteValueDictionary(RouteValues), HtmlAttributes, MergeRouteValuesWithCurrent);
        }

        /// <summary>
        /// Overload ActionLink to allow the use of HTML by
        /// wrapping in the HtmlContainer class
        /// </summary>
        /// <param name="HtmlHelper"></param>
        /// <param name="Html"></param>
        /// <param name="Action"></param>
        /// <param name="RouteValues"></param>
        /// <param name="HtmlAttributes"></param>
        /// <param name="MergeRouteValuesWithCurrent"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink(this HtmlHelper HtmlHelper, HtmlContainer Html, string Action, RouteValueDictionary RouteValues = null, IDictionary<string, object> HtmlAttributes = null, bool MergeRouteValuesWithCurrent = true) {
            return HtmlHelper.ActionLink(Html, Action, null, RouteValues, HtmlAttributes, MergeRouteValuesWithCurrent);
        }

        /// <summary>
        /// Overload ActionLink to allow the use of HTML by
        /// wrapping in the HtmlContainer class
        /// </summary>
        /// <param name="HtmlHelper"></param>
        /// <param name="Html"></param>
        /// <param name="Action"></param>
        /// <param name="Controller"></param>
        /// <param name="RouteValues"></param>
        /// <param name="HtmlAttributes"></param>
        /// <param name="MergeRouteValuesWithCurrent"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink(this HtmlHelper HtmlHelper, HtmlContainer Html, string Action, string Controller, object RouteValues = null, IDictionary<string, object> HtmlAttributes = null, bool MergeRouteValuesWithCurrent = true) {
            return HtmlHelper.ActionLink(Html, Action, Controller, new RouteValueDictionary(RouteValues), HtmlAttributes, MergeRouteValuesWithCurrent);
        }

        /// <summary>
        /// Overload ActionLink to allow the use of HTML by
        /// wrapping in the HtmlContainer class
        /// </summary>
        /// <param name="HtmlHelper"></param>
        /// <param name="Html"></param>
        /// <param name="Action"></param>
        /// <param name="Controller"></param>
        /// <param name="RouteValues"></param>
        /// <param name="HtmlAttributes"></param>
        /// <param name="MergeRouteValuesWithCurrent"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink(this HtmlHelper HtmlHelper, HtmlContainer Html, string Action, string Controller, RouteValueDictionary RouteValues = null, IDictionary<string, object> HtmlAttributes = null, bool MergeRouteValuesWithCurrent = true) {
            
            // Merge with current route values
            if (MergeRouteValuesWithCurrent) {
                RouteValues = HtmlHelper.ViewContext.RouteQueryValues().Merge(RouteValues);
            }

            string Holder = Guid.NewGuid().ToString();
            string Anchor = HtmlHelper.ActionLink(Holder, Action, Controller, RouteValues, HtmlAttributes).ToString();
 
            return MvcHtmlString.Create(Anchor.Replace(Holder, Html.ToString()));
        } 
    }
}
